package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad Pai.
 */
public class AccionesPai extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreCrear_pai_profesor.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_pai_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_pai_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPai fFPai = FPai.clear(mapping, request);
             
            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPai.setListProfesor(list);
            }

            fFPai.setProfesor(Long.parseLong((String)request.getParameter("idProfesor_super")));

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_pai.
     * Prepara la vista de actualización de una entidad.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_pai(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pai", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPai = lookup_Long(request, "idPai");
            
            FPai fFPai = FPai.clear(mapping, request);
            Pai pai = (Pai)s.load(Pai.class, idPai);
            fFPai.setIdPai(pai.getIdPai());

            fFPai.setProfesor(pai.getProfesor().getIdProfesor());

            fFPai.setCi(pai.getCi());
            fFPai.setFec_Ini(pai.getFec_Ini().toString());
            fFPai.setFec_Fin(pai.getFec_Fin().toString());
            fFPai.setArticulo8(pai.getArticulo8());

            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPai.setListProfesor(list);
            }


            request.getSession().setAttribute("idProfesor_super", fFPai.getProfesor());

            request.getSession().setAttribute("idPai", idPai);
            request.getSession().setAttribute("idPai_super", idPai);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_pai.
     * Registro de actualizaciones de Pai.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_pai(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pai", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPai fFPai = (FPai)form;
            Pai pai = (Pai)s.load(Pai.class, fFPai.getIdPai());
            pai.setCi(fFPai.getCi());
            pai.setFec_Ini(new ve.usb.cohesion.runtime.LocalDate(fFPai.getFec_Ini()));
            pai.setFec_Fin(new ve.usb.cohesion.runtime.LocalDate(fFPai.getFec_Fin()));
            pai.setArticulo8(fFPai.getArticulo8());


            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPai.setListProfesor(list);
            }


            request.getSession().setAttribute("idProfesor_super", fFPai.getProfesor());

            request.getSession().setAttribute("idPai", pai.getIdPai());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_pai.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_pai.
     * Elimina el elemento seleccionado de Pai.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_pai(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APre_profesor", "V_pai", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPai = lookup_Long(request, "idPai");
            Pai pai = (Pai)s.load(Pai.class, idPai);

            long idProfesor_super = pai.getProfesor().getIdProfesor();
            pai.getProfesor().removeFromSet_Pai(pai);

            try {
                s.flush();
                request.getSession().setAttribute("idPai", idPai);

                request.getSession().setAttribute("idProfesor_super", idProfesor_super);

            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_pai.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_pai.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_pai.
     * Registro de la creación de un elemento de Pai.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_pai(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pai", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPai fFPai = (FPai)form;
            Pai pai = new Pai();
            pai.setCi(fFPai.getCi());
            pai.setFec_Ini(new ve.usb.cohesion.runtime.LocalDate(fFPai.getFec_Ini()));
            pai.setFec_Fin(new ve.usb.cohesion.runtime.LocalDate(fFPai.getFec_Fin()));
            pai.setArticulo8(fFPai.getArticulo8());


            {Profesor obj = (Profesor)s.load(Profesor.class, fFPai.getProfesor());
             obj.addToSet_Pai(pai);
            }

            s.flush();
            fFPai.setIdPai(pai.getIdPai());
            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPai.setListProfesor(list);
            }


            //fFPai.setProfesor(pai.getProfesor().getIdProfesor());
            request.getSession().setAttribute("idProfesor_super", fFPai.getProfesor());

            request.getSession().setAttribute("idPai", pai.getIdPai());
            request.getSession().setAttribute("idPai_super", pai.getIdPai());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_pai.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }



}